public class ListNode {

    public MyList head;
    static class  MyList{
        public int val;
        public MyList next;
        public  MyList(int val){
            this.val = val;

        }
    }

        public void addFirst(int data) {
            MyList node = new MyList(data);
            node.next = head;
            head = node;
        }
    public MyList reverseList() {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        MyList cur = head;
        MyList curN = head.next;
        cur = curN;
        head.next = null;
        while(cur != null){
            curN = curN.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }


    public void display() {
        MyList cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
